﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.JCI.Common.Utility;
using Microsoft.JCI.Common.Entity;
using Microsoft.JCI.Bll;
using System.Data;

namespace Microsoft.JCI.Portal.SysManage
{
    public partial class UserList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindUserList(1);
            }
        }
        private void BindUserList(int pageindex)
        {
            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            int RecordCount = 0;
            List<SysUserEntity> list = new SystemBll().GetAll<SysUserEntity>(parms, "Account asc", AspNetPager1.PageSize, pageindex, out RecordCount);

            RepeaterUsers.DataSource = list;
            RepeaterUsers.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        protected void RepeaterUsers_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "refersh")
            {
                BindUserList(1);
            }
        }
        protected void lnkbtnDelete_Click(object sender, EventArgs e)
        {
            if (userids.Value.Trim() != "") {
                string[] useridsArray = userids.Value.Trim().Split(new char[] { ';' });
                for (int index = 0; index < useridsArray.Length; index++) {
                    SysUserEntity entity = new SystemBll().Get<SysUserEntity>(new Guid(useridsArray[index]));
                    new SystemBll().LogicDelete(entity);
                    BindUserList(1);
                }
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {

            BindUserList(AspNetPager1.CurrentPageIndex);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            int RecordCount = 0;
            MasterDataBll bll = new MasterDataBll();
            List<SysUserEntity> dt = bll.GetAll<SysUserEntity>(GetQueryCondition());
            RepeaterUsers.DataSource = dt;
            RepeaterUsers.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }
        private List<SqlDbParameter> GetQueryCondition()
        {
            List<SqlDbParameter> listParameters = new List<SqlDbParameter>();
            SqlDbParameter Parameter = null;

            if (!string.IsNullOrEmpty(this.txtVenderName.Text.Trim()))
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "UserName";
                Parameter.ColumnType = DbType.String;
                Parameter.ParameterName = "@UserName";
                Parameter.ParameterValue = this.txtVenderName.Text.Trim();
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Like;
                listParameters.Add(Parameter);
            }

            if (!string.IsNullOrEmpty(this.txtVenderAccount.Text.Trim()))
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "Account";
                Parameter.ColumnType = DbType.String;
                Parameter.ParameterName = "@Account";
                Parameter.ParameterValue = this.txtVenderAccount.Text.Trim();
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Like;
                listParameters.Add(Parameter);
            }
            return listParameters;
        }
    }
}
